﻿<?php 
	if(empty($_GET['complete']))
	{
		$dbHost = trim($_POST['dbHost']);
		$dbPort = trim($_POST['dbPort']);
		$dbName = trim($_POST['dbName']);
		$dbUrl = empty($dbPort) || $dbPort == 3306 ? $dbHost.':3306' : $dbHost . ':' . $dbPort;
		$dbUser = trim($_POST['dbUser']);
		$dbPwd = trim($_POST['dbPwd']);
		$sysUser = trim($_POST['sysUser']);
		$sysPwd = trim($_POST['sysPwd']);
		$tbl_pre = empty($_POST['tbl_pre'])?trim(empty($_POST['tbl_pre'])):'dqt_';

		// 生成config文件内容
		$dbconfig_str = "<?php\n";
		$dbconfig_str .= "/**\n";
		$dbconfig_str .= " * \n";
		$dbconfig_str .= " * \n";
		$dbconfig_str .= " * 大连东丘贸易有限公司\n";
		$dbconfig_str .= " * http://www.dq187.com\n";
		$dbconfig_str .= " * \n";
		$dbconfig_str .= " * \n";
		$dbconfig_str .= " * Author: Song Dongfang\n";
		$dbconfig_str .= " * Date: 2014-06-23\n";
		$dbconfig_str .= " * /\n\n";
		
		$dbconfig_str .= "// database host\n";
		$dbconfig_str .= '$dbHost   = "' . $dbHost . '";' . "\n\n";
		
		$dbconfig_str .= "// database port\n";
		$dbconfig_str .= '$dbPort   = "' . $dbPort . '";' . "\n\n";

		$dbconfig_str .= '$dbUrl   = "' . $dbUrl . '";' . "\n\n";
		
		$dbconfig_str .= "// database name\n";
		$dbconfig_str .= '$dbName   = "' . $dbName . '";' . "\n\n";
		
		$dbconfig_str .= "// database username\n";
		$dbconfig_str .= '$dbUser   = "' . $dbUser . '";' . "\n\n";
		
		$dbconfig_str .= "// database password\n";
		$dbconfig_str .= '$dbPwd   = "' . $dbPwd . '";' . "\n\n";
		
		$dbconfig_str .= "// table prefix\n";
		$dbconfig_str .= '$tbl_pre   = "' . $tbl_pre . '";' . "\n\n";

		$dbconfig_str .= "?>";
		
		try
		{
			$db_config = ROOT_PATH . 'include/dbconfig.php';
			$fopen_config = fopen($db_config, "w+");
			fwrite($fopen_config, $dbconfig_str);
			fclose($fopen_config);
		}
		catch(Exception $ex)
		{
			exit('写入数据库配置文件失败，请检查后重试！ error: ' . $ex->getMessage());
		}

		$conn = @mysql_connect($dbUrl, $dbUser, $dbPwd);
		if(!$conn)
		{
			die('连接数据库失败：' . mysql_error());
		}
		
		if(!@mysql_query('create database if not exists `' . $dbName . '` default charset utf8 COLLATE utf8_general_ci'))
		{
			die('创建数据库失败：' . mysql_error());
		}
		mysql_select_db($dbName, $conn);
		
		// 读取SQL文件到一个字符串中
		$sql = file_get_contents(ROOT_PATH . 'install/sql/dq187.sql');
		
		
		
		// 进行安装的常规替换
		$sql = preg_replace('/dqt_/Ums', $tbl_pre, $sql);
		
		// 进行安装的常规替换
		$sql_head = "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n";
		$sql_head .= "SET time_zone = '+08:00';\n\n\n";
		
		$sql_head .= "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n";
		$sql_head .= "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n";
		$sql_head .= "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n";
		$sql_head .= "/*!40101 SET NAMES utf8 */;\n\n";
		
		$sql = $sql_head . $sql;

		// 创建表、执行.sql文件
		$check_result = $install->sql_execute($sql, $conn);
		if(!$check_result)
		{
			exit("初始化建表失败!");
		}
		// 初始化数据

		// 生成管理员
		$table_admin = $tbl_pre . "admin";

		$sql_admin_select = "select * from " . $table_admin . " where upper(user_name) = upper('" . $sysUser . "')";
		$check_result = mysql_query($sql_admin_select, $conn);
		if(!$check_result)
		{
			exit("超级管理员校验失败!");
		}

		if(mysql_num_rows($check_result) <= 0)
		{
			$sql_admin_insert = "";
			$sql_admin_insert .= "insert into " .  $table_admin . " (user_name, password, action_list, add_time) ";
			$sql_admin_insert .= " values ( ";
			$sql_admin_insert .= "'" . $sysUser . "', ";
			$sql_admin_insert .= "'" . md5($sysPwd) . "', ";
			$sql_admin_insert .= "'ALL', ";
			$sql_admin_insert .= time();
			$sql_admin_insert .= ");";
			
			$check_result = mysql_query($sql_admin_insert, $conn);
			if(!$check_result)
			{
				exit("初始化超级管理员数据失败! error:" . mysql_error());
			}
			
		}

		// 初始化系统信息
		$table_config = $tbl_pre . "config";
		/* 写入 hash_code，做为网站唯一性密钥 */
		$hash_code = md5(md5(time()) . md5(md5(ROOT_URL . $dbHost . $dbName . $dbUser . $dbPwd)));
		
		$sql_config_select = "select * from " . $table_config . " where upper(name) = upper('hash_code')";
		$check_result = mysql_query($sql_config_select, $conn);
		if(!$check_result)
		{
			exit("网站配置校验码校验失败!");
		}
		
		if(mysql_num_rows($check_result) <= 0)
		{
			$sql_config_insert = "";
			$sql_config_insert .= "insert into " .  $table_config . " (name, value, type) ";
			$sql_config_insert .= " values ( ";
			$sql_config_insert .= "'hash_code', ";
			$sql_config_insert .= "'" . $hash_code . "', ";
			$sql_config_insert .= "'hidden'";
			$sql_config_insert .= ");";
			
			$check_result = mysql_query($sql_config_insert, $conn);
			if(!$check_result)
			{
				exit("初始化网站配置校验码失败! error:" . mysql_error());
			}
		}
		
		// 写入安装锁定文件爱你
		try
		{
			$file_install_lock = ROOT_PATH . 'include/install.lock';
			$fopen_config = fopen($file_install_lock, "w+");
			fwrite($fopen_config, "");
			fclose($fopen_config);
		}
		catch(Exception $ex)
		{
			exit('写入安装锁定文件失败，请检查后重试！ error: ' . $ex->getMessage());
		}
		
		header("Location: index.php?step=4&complete=1&sysUser=". $sysUser. "&sysPwd=". $sysPwd);
		exit();
	}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>东丘管理系统安装向导</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src="./js/jquery.min.js" language="javascript" type="text/javascript"></script>
        <script src="./js/jquery.form.js" language="javascript" type="text/javascript"></script>
        <script src="./js/jquery.validate.js" language="javascript" type="text/javascript"></script>
        <link href="./css/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div class="main">
            <h5>欢迎使用东丘管理系统</h5>

            <div class="title">
				<ul>
                    <?php  foreach($steps as $key=>$res){ ?>
                    <li <?php if($step==$key):?> class="on"<?php endif;?> ><?php echo $res?></li>
                    <?php } ?>
                </ul>
			</div>

            <div class="box">
				<div class="b1">
					<div class="right">

						<h1><font color="red">恭喜您，安装成功！</font></h1>	 
						<div id="setupOK"> 
							<div class="nr">

								<div class="ok1">
									<a href="<?php echo ROOT_URL ?>/admin/index.php" >进入后台管理 默认帐号 <?php echo $_GET['sysUser'] ?> 默认密码 <?php echo $_GET['sysPwd'] ?></a> 
								</div>					
								<div class="ok2">
									<font color="#FF0000">在进入后台前，有如下需要注意的地方</font>：<br/>
									<span>*</span>进入后台以后，第一件事是<font color="#FF0000">更新网站缓存</font>，不然有些功能不正常！<br/>
									<span>*</span>为了您站点的安全，安装完成后即可将网站根目录下的“install”文件夹删除。
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="power">Powered by <a href="#" target="_blank">大连东丘贸易有限公司</a> 
				</div>
			</div>
		</div>
	</body>
</html>